package com.lazily.set_;


import java.util.HashSet;
import java.util.Objects;

/**
 * @Author:  GZQ
 * @date:  2025/5/26 22:51
 * @description: HashSet练习
 * @version: 1.0
 */


@SuppressWarnings({"all"})
public class HashSetExercise {
	// 程序入口
	public static void main(String[] args) {
		/*
		定义一个Employee类, 该类包含: private成员属性name, age要求：
		1. 创建3个Employee 对象放入HashSet中
		2. 当name 和 age 的值相同时, 认为是相同员工, 不能添加到HashSet集合中
		 */

		HashSet hashSet = new HashSet();
		hashSet.add(new Employee("jack", 28));
		hashSet.add(new Employee("mary", 29));
		hashSet.add(new Employee("jack", 28));
		System.out.println("员工信息: " + hashSet); // 加入不成功
	}
}


class Employee {
	private String name;
	private int age;

	public Employee(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return  "名字：" +  name + "\t" +
				"年龄：" + age + "\n";
	}

	// 如果name 和 age 相同, 则返回相同的hash值


	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Employee employee = (Employee)o;
		return age == employee.age && Objects.equals(name, employee.name);
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, age);
	}
}
